{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import chemview\n",
    "import time\n",
    "\n",
    "from chemview import RepresentationViewer\n",
    "from IPython.display import display"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Points\n",
    "initialization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "coordinates = np.array([[0.0, 1.1, 0.1], [1, 0, 0]], 'float32')\n",
    "colors = [0xFFFFFF, 0xFF999F]\n",
    "sizes = [1.0, 2.0]\n",
    "\n",
    "rv = RepresentationViewer(300, 300)\n",
    "point_id = rv.add_representation('points', {'coordinates': coordinates, 'colors': colors, 'sizes': sizes})\n",
    "rv"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "update"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for i in range(30):\n",
    "    rv.update_representation(point_id, {'coordinates': coordinates + [np.sin(0.1 * i), np.cos(0.1 * i), np.cos(0.1 * i)]})\n",
    "    time.sleep(0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lines"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "coordinates = np.array([[0, 0, 1], [1, 0, 0], [0, 1, 0]], 'float32')\n",
    "colors = np.array([0xFF0000, 0x00FF00, 0x0000FF])\n",
    "\n",
    "rv = RepresentationViewer()\n",
    "lines_id = rv.add_representation('lines', {'startCoords': coordinates[[0, 1, 2]],\n",
    "                                           'endCoords': coordinates[[1, 2, 0]],\n",
    "                                           'startColors': colors[[0, 1, 2]].tolist(),\n",
    "                                           'endColors': colors[[1, 2, 0]].tolist()})\n",
    "rv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for i in range(30):\n",
    "    offset = [np.sin(0.1 * i), np.cos(0.1 * i), np.cos(0.1 * i)]\n",
    "    rv.update_representation(lines_id, {'startCoords': coordinates[[0, 1, 2]] + offset,\n",
    "                                        'endCoords': coordinates[[1, 2, 0]] + offset})\n",
    "    time.sleep(0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Spheres"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "coordinates = np.array([[0.0, 1.1, 0.1], [1, 0, 0]], 'float32')\n",
    "colors = [0xFFFFFF, 0xFF999F]\n",
    "sizes = [1.0, 2.0]\n",
    "\n",
    "rv = RepresentationViewer()\n",
    "spheres_id = rv.add_representation('spheres', {'coordinates': coordinates, \n",
    "                                               'colors': colors, \n",
    "                                               'radii': sizes, \n",
    "                                               'resolution': 32})\n",
    "rv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for i in range(30):\n",
    "    offset = [np.sin(0.1 * i), np.cos(0.1 * i), np.cos(0.1 * i)]\n",
    "    rv.update_representation(spheres_id, {'coordinates': coordinates + offset})\n",
    "    time.sleep(0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Cylinders"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "coordinates = np.array([[0, 0, 1], [1, 0, 0], [0, 1, 0]], 'float32')\n",
    "colors = np.array([0xFF0000, 0x00FF00, 0x0000FF])\n",
    "\n",
    "rv = RepresentationViewer()\n",
    "cylinders_id = rv.add_representation('cylinders', {'startCoords': coordinates[[0, 1, 2]],\n",
    "                                           'endCoords': coordinates[[1, 2, 0]],\n",
    "                                           'radii': [0.2, 0.1, 0.1],\n",
    "                                           'colors': colors[[0, 1, 2]].tolist()})\n",
    "rv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for i in range(30):\n",
    "    offset = [np.sin(0.1 * i), np.cos(0.1 * i), np.cos(0.1 * i)]\n",
    "    rv.update_representation(cylinders_id, {'startCoords': coordinates[[0, 1, 2]] + offset,\n",
    "                                        'endCoords': coordinates[[1, 2, 0]] + offset})\n",
    "    time.sleep(0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SmoothTube"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# We want to draw a helix\n",
    "r = 0.3\n",
    "coordinates = []\n",
    "for t in range(50):\n",
    "    coordinates.append([r * np.cos(t),\n",
    "                        r * np.sin(t),\n",
    "                        t * 0.1])\n",
    "\n",
    "coordinates = np.array(coordinates, 'float32')\n",
    "\n",
    "rv = RepresentationViewer()\n",
    "smooth_id = rv.add_representation('smoothtube', {'coordinates': coordinates, \n",
    "                                                 'radius': 0.05, \n",
    "                                                 'color' : 0xff99ff,\n",
    "                                                 'resolution': 8})\n",
    "rv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for i in range(30):\n",
    "    offset = [np.sin(0.1 * i), np.cos(0.1 * i), np.cos(0.1 * i)]\n",
    "    rv.update_representation(smooth_id, {'coordinates': coordinates + offset})\n",
    "    time.sleep(0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# SmoothLine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# We want to draw a helix\n",
    "r = 0.3\n",
    "coordinates = []\n",
    "for t in range(50):\n",
    "    coordinates.append([r * np.cos(t),\n",
    "                        r * np.sin(t),\n",
    "                        t * 0.1])\n",
    "\n",
    "coordinates = np.array(coordinates, 'float32')\n",
    "\n",
    "rv = RepresentationViewer()\n",
    "smooth_id = rv.add_representation('smoothline', {'coordinates': coordinates, 'resolution': 8})\n",
    "rv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for i in range(30):\n",
    "    offset = [np.sin(0.1 * i), np.cos(0.1 * i), np.cos(0.1 * i)]\n",
    "    rv.update_representation(smooth_id, {'coordinates': coordinates + offset})\n",
    "    time.sleep(0.1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TrajectoryControl widget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Current frame 1\n",
      "Current frame 2\n",
      "Current frame 3\n",
      "Current frame 4\n",
      "Current frame 5\n",
      "Current frame 6\n",
      "Current frame 7\n",
      "Current frame 8\n",
      "Current frame 9\n",
      "Current frame 1\n",
      "Current frame 2\n",
      "Current frame 3\n",
      "Current frame 4\n",
      "Current frame 5\n",
      "Current frame 6\n",
      "Current frame 7\n",
      "Current frame 8\n",
      "Current frame 9\n",
      "Current frame 8\n",
      "Current frame 7\n",
      "Current frame 6\n",
      "Current frame 5\n",
      "Current frame 4\n",
      "Current frame 3\n",
      "Current frame 2\n",
      "Current frame 1\n",
      "Current frame 0\n",
      "Current frame 1\n",
      "Current frame 2\n",
      "Current frame 3\n",
      "Current frame 4\n",
      "Current frame 5\n",
      "Current frame 6\n",
      "Current frame 7\n",
      "Current frame 8\n",
      "Current frame 9\n"
     ]
    }
   ],
   "source": [
    "from IPython.display import display\n",
    "from chemview import TrajectoryControls\n",
    "controls = TrajectoryControls(10) # 10 frames\n",
    "\n",
    "def callback(frame):\n",
    "    print(\"Current frame %d\" % frame)\n",
    "\n",
    "controls.on_frame_change(callback)\n",
    "display(controls)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
